﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Empires.CRL.Logger;

namespace Empires.PL.PageHandler
{
    public class HandlerErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("应用程序发生异常：\r\n{0}\r\n*********************************\r\n于：{1},HttpMethod:{2}\r\n*********************************\r\n");
            if(filterContext.Exception.InnerException != null)
            {
                sb.Append("内部错误消息：{3}");
            }
            if (filterContext.Exception.InnerException != null)
            {
                EmpiresLogger.ErrorLog(String.Format(sb.ToString(),
                    filterContext.Exception.Message, filterContext.HttpContext.Request.RawUrl,
                    filterContext.HttpContext.Request.HttpMethod,
                    filterContext.Exception.InnerException.Message), filterContext.Controller.ToString());
            }
            else
            {
                EmpiresLogger.ErrorLog(String.Format(sb.ToString(),
                   filterContext.Exception.Message, filterContext.HttpContext.Request.RawUrl,
                   filterContext.HttpContext.Request.HttpMethod), filterContext.Controller.ToString());
            }
            base.OnException(filterContext);
        }
    }
}
